package com.eliota.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eliota.common.Constants;
import com.eliota.common.R;
import com.eliota.controller.dto.DeviceDto;
import com.eliota.entity.Device;
import com.eliota.entity.ProjectDevice;
import com.eliota.mapper.ProjectDeviceMapper;
import com.eliota.mapper.ProjectMapper;
import com.eliota.service.ProjectDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-04-05
 */

@Slf4j
@RestController
@RequestMapping("/project-device")
public class ProjectDeviceController {

    @Resource
    private ProjectDeviceService projectDeviceService;


    @Resource
    private ProjectDeviceMapper projectDeviceMapper;

    /**
     * 设备表请求，设备表借阅状态变更，设备用户表添加设备用户编号
     * @param deviceDto
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody DeviceDto deviceDto){
        projectDeviceService.saveBorrow(deviceDto);

        return R.success();
    }


    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String  userName){
        Map<String,Object> params = new HashMap<>();
       /* if(StrUtil.isNotBlank(userName)){
            params.put("userName",userName);
        }*/
        params.put("userName",userName);
        params.put("startIndex",(pageNum - 1) * pageSize);
        params.put("pageSize",pageSize);

        return R.success(projectDeviceService.selectProjectDevice(params));
    }

    @GetMapping("/total")
    public R getTotal(){
        QueryWrapper<ProjectDevice> projectDeviceQueryWrapper = new QueryWrapper<>();
        projectDeviceQueryWrapper.eq("status",1);
        return R.success(projectDeviceMapper.selectCount(projectDeviceQueryWrapper));
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        projectDeviceService.deleteById(id);
        return R.success();
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        projectDeviceService.deleteBatchByIds(ids);
        return R.success();
    }


    @PostMapping("/back")
    public R backDevice(@RequestBody ProjectDevice projectDevice){
        projectDeviceService.backDevice(projectDevice);
        return R.success();
    }


}
